using System;
using System.Web.UI;
using CodeStory.Core.Authentication;
using System.Web;

namespace CodeStory.Core.UI
{
	/// <summary>
	/// 
	/// </summary>
	public class BasePage : Page
	{
		private UserCookie userCookie = null;
		/// <summary>
		/// Gets the user cookie.
		/// </summary>
		/// <value>The user cookie.</value>
		public UserCookie UserCookie {
			get {
				if (userCookie == null) {
					userCookie = new UserCookie(Context);
				}
				return userCookie;
			}
		}

		/// <summary>
		/// Gets the name of the skin.
		/// </summary>
		/// <value>The name of the skin.</value>
		public string SkinName {
			get {
				string name = UserCookie.SkinName;
				if (string.IsNullOrEmpty(name)) {
					name = CodeStoryConfig.DefaultSkinName;
				}
				return name;
			}
		}

		/// <summary>
		/// Raises the <see cref="E:System.Web.UI.Page.PreRenderComplete"></see> event after the <see cref="M:System.Web.UI.Page.OnPreRenderComplete(System.EventArgs)"></see> event and before the page is rendered.
		/// </summary>
		/// <param name="e">An <see cref="T:System.EventArgs"></see> that contains the event data.</param>
		protected override void OnPreRenderComplete(EventArgs e) {
			UserCookie.Save();
			base.OnPreRenderComplete(e);
		}

		/// <summary>
		/// Checks the permission.
		/// </summary>
		/// <param name="resourceid">The resourceid.</param>
		protected void CheckPermission(string resourceid) {
			bool result = CodeStoryAuthenticationManager.ValidatePermission(resourceid,"page", (int)PageAction.View, UserContext.Current.Roles);
			AfterCheckPermission(result);
		}

		/// <summary>
		/// Afters the check permission.
		/// </summary>
		/// <param name="result">if set to <c>true</c> [result].</param>
		protected virtual void AfterCheckPermission(bool result) {
		}

		/// <summary>
		/// Raises the <see cref="E:System.Web.UI.TemplateControl.Error"></see> event.
		/// </summary>
		/// <param name="e">An <see cref="T:System.EventArgs"></see> that contains the event data.</param>
		protected override void OnError(EventArgs e) {
			string url = string.Empty;
			Uri u = Request.UrlReferrer;
			if (u != null) {
				url = u.ToString();
			}
			Server.Transfer("~/error.aspx?url="+url,true);
		}
		#region Navigation

		protected void Navigate(Type type) {
			Navigate(type, null);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="type"></param>
		/// <param name="querystring"></param>
		protected void Navigate(Type type, params string[] querystring) {
			ILayoutPage page = this.Page as LayoutPage;
			if (page == null) {
				page = HttpContext.Current.Handler as ILayoutPage;
			}
			if (page == null) {
				return;
			}
			foreach (Control c in page.UserControlsInPage) {
				Type t = c.GetType();
				if (t.IsSubclassOf(type) || t.Equals(type)) {
					bool flag = CheckQuery(querystring);
					if (flag) {
						return;
					} else {
						break;
					}
				}
			}

			NavigationPortletAttribute attr = Attribute.GetCustomAttribute(type, typeof(NavigationPortletAttribute)) as NavigationPortletAttribute;
			if (attr == null) {
				return;
			}
			string query = string.Empty;
			if (querystring != null) {
				query = string.Join("&", querystring);
			}
			if (query.Length > 0) {
				query = "?" + query;
			}

			Response.Redirect(string.Format("~/{0}.portlet.aspx{1}", attr.NavigationName, query));
		}

		private bool CheckQuery(string[] querystring) {
			bool flag = true;
			if (querystring != null) {
				foreach (string str in querystring) {
					string[] pa = str.Split('=');
					if (pa.Length == 2) {
						string s = Request.QueryString[pa[0]];
						if (!string.Equals(s, pa[1], StringComparison.OrdinalIgnoreCase)) {
							flag = false;
							break;
						}
					} else {
					}
				}
			}
			return flag;
		}
		#endregion
	}
}